export default {
    data (){
        return {
            gdMap: undefined,
            gdMapInstance: undefined
        }
    },

    methods: {
        __createGdMap (containerId, options = {}){
            const _self = this;

            return new Promise(resolve => {
                if(!window.AMap){
                    this.__initGdMap().then(callback);
                }else{
                    callback();
                }

                function callback() {
                    let instance = new window.AMap.Map(containerId, options);

                    _self.gdMapInstance = instance;
                    _self.gdMap = window.AMap;

                    resolve({
                        Map: window.AMap,
                        instance
                    });
                }
            });
        },

        __initGdMap (){
            return new Promise(resolve => {
                let script = document.createElement('script');

                script.src = 'https://webapi.amap.com/maps?v=1.4.15&key=b4b976ef3b198b5b1c59d1d625a15069&plugin=AMap.Autocomplete,AMap.PlaceSearch,AMap.Geocoder&callback=__initGdMap';

                document.body.appendChild(script);

                window.__initGdMap = () => resolve();
            });
        }
    }
}
